package com.IntelligentCommunity.community.service.impl;

import com.IntelligentCommunity.common.redis.service.RedisService;
import com.IntelligentCommunity.common.security.utils.SecurityUtils;
import com.IntelligentCommunity.community.domain.RealDistrict;
import com.IntelligentCommunity.community.domain.Tree;
import com.IntelligentCommunity.community.mapper.RealDistrictMapper;
import com.IntelligentCommunity.community.service.IRealDistrictService;
import com.IntelligentCommunity.system.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 小区Service业务层处理
 *
 * @author zzpjx
 * @date 2024-07-19
 */
@Service
public class RealDistrictServiceImpl implements IRealDistrictService
{
    @Autowired
    private RealDistrictMapper realDistrictMapper;

    @Autowired
    private RedisService redisService;

    public static final String REDIS_KEY_PREFIX_REAL_DISTRICT = "REAL_DISTRICT:";

    /**
     * 查询小区
     *
     * @param id 小区主键
     * @return 小区
     */
    @Override
    public RealDistrict selectRealDistrictById(Long id)
    {
        RealDistrict realDistrict = realDistrictMapper.selectRealDistrictById(id);
        return realDistrict;
    }

    /**
     * 查询小区列表
     *
     * @param realDistrict 小区
     * @return 小区
     */
    @Override
    public List<RealDistrict> selectRealDistrictList(RealDistrict realDistrict) {

        LoginUser loginUser = SecurityUtils.getLoginUser();

        if (loginUser.getUserDistrictPermissions().contains("all")) {
            return realDistrictMapper.selectRealDistrictList(realDistrict, null);
        } else {
            return realDistrictMapper.selectRealDistrictList(realDistrict, loginUser.getUserDistrictPermissions());
        }
    }


    /**
     * 新增小区
     *
     * @param realDistrict 小区
     * @return 结果
     */
    @Override
    public int insertRealDistrict(RealDistrict realDistrict)
    {
        redisService.deleteObject(REDIS_KEY_PREFIX_REAL_DISTRICT);
        return realDistrictMapper.insertRealDistrict(realDistrict);
    }

    /**
     * 修改小区
     *
     * @param realDistrict 小区
     * @return 结果
     */
    @Override
    public int updateRealDistrict(RealDistrict realDistrict)
    {
        redisService.deleteObject(REDIS_KEY_PREFIX_REAL_DISTRICT);
        return realDistrictMapper.updateRealDistrict(realDistrict);
    }

    /**
     * 批量删除小区
     *
     * @param ids 需要删除的小区主键
     * @return 结果
     */
    @Override
    public int deleteRealDistrictByIds(Long[] ids)
    {
        redisService.deleteObject(REDIS_KEY_PREFIX_REAL_DISTRICT);
        return realDistrictMapper.deleteRealDistrictByIds(ids);
    }

    /**
     * 删除小区信息
     *
     * @param id 小区主键
     * @return 结果
     */
    @Override
    public int deleteRealDistrictById(Long id)
    {
        redisService.deleteObject(REDIS_KEY_PREFIX_REAL_DISTRICT);
        return realDistrictMapper.deleteRealDistrictById(id);
    }

    /**
     * 功能：小区列表查询
     * @return
     */
    @Override
    public List<RealDistrict> districtList() {

        List<RealDistrict> list = realDistrictMapper.districtList();
        return list;
    }

    @Override
    public List<RealDistrict> selectRealDistrictByAreaId(Long areaId) {
        List<RealDistrict> list = realDistrictMapper.selectRealDistrictByAreaId(areaId);
        return list;
    }

    @Override
    public List<Tree> selectTree() {
        List<Tree> list = realDistrictMapper.selectTree();
        return list;
    }

    @Override
    public void insertRealDistrictList(List<RealDistrict> list) {
        realDistrictMapper.insertRealDistrictList(list);
    }

    @Override
    public RealDistrict selectRealDistrictInfoById(Long id) {
        return realDistrictMapper.selectRealDistrictInfoById(id);
    }

    @Override
    public List<RealDistrict> getDistrictByAreaId(Long id) {
        return id == -1 ? null: realDistrictMapper.getDistrictByAreaId(id);
    }
}
